What is chroma-js?
chroma-js is a JavaScript library for color conversions and color scales. It provides a wide range of functionalities for color manipulation, including color creation, color scales, color interpolation, and color analysis.
What are chroma-js's main functionalities?
Color Creation
You can create colors using various input formats such as named colors, hex codes, RGB, HSL, etc. The example demonstrates creating a color from a named color and converting it to a hex code.
const chroma = require('chroma-js');
const color = chroma('red');
console.log(color.hex()); // #FF0000
Color Scales
Chroma-js allows you to create color scales that can interpolate between multiple colors. The example shows creating a grayscale scale and getting the color at the midpoint.
const chroma = require('chroma-js');
const scale = chroma.scale(['white', 'black']);
console.log(scale(0.5).hex()); // #808080
Color Interpolation
You can interpolate between two colors using chroma-js. The example demonstrates mixing red and blue to get a purple color.
const chroma = require('chroma-js');
const color1 = chroma('red');
const color2 = chroma('blue');
const interpolatedColor = chroma.mix(color1, color2, 0.5);
console.log(interpolatedColor.hex()); // #800080
Color Analysis
Chroma-js provides methods to analyze colors, such as getting the luminance. The example shows how to get the luminance of the color orange.
const chroma = require('chroma-js');
const color = chroma('orange');
console.log(color.luminance()); // 0.5
Other packages similar to chroma-js
color
The 'color' package is a JavaScript library for color conversion and manipulation. It supports a wide range of color models and provides methods for color transformations. Compared to chroma-js, it is more focused on color conversions and less on color scales and interpolation.
tinycolor2
TinyColor is a small color manipulation and conversion library. It provides similar functionalities to chroma-js, such as color creation, manipulation, and conversion. However, it is designed to be lightweight and may not have as extensive features for color scales and interpolation as chroma-js.
d3-color
d3-color is part of the D3.js library and provides color manipulation and conversion functionalities. It integrates well with other D3 modules for data visualization. While it offers similar color manipulation features, it is more commonly used within the context of D3 visualizations.
Chroma.js
Chroma.js is a tiny JavaScript library (14kB) for all kinds of color conversions and color scales.
Usage
Initiate and manipulate colors:
chroma('#D4F880').darken().hex();
Working with color scales is easy, too:
scale = chroma.scale(['white', 'red']);
scale(0.5).hex();
Lab/Lch interpolation looks better than RGB
chroma.scale(['white', 'red']).mode('lab');
Custom domains! Quantiles! Color Brewer!!
chroma.scale('RdYlBu').domain(myValues, 7, 'quantiles');
And why not use logarithmic color scales once in your life?
chroma.scale(['lightyellow', 'navy']).domain([1, 100000], 7, 'log');
Like it?
Why not dive into the interactive documentation (there's a static version, too). You can download chroma.min.js or use the hosted version on cdnjs.com.
You can use it in node.js, too!
npm install chroma-js
Or you can use it in SASS using chromatic-sass!
Build instructions
First clone the repository and install the dev dependencies:
git clone git@github.com:gka/chroma.js.git
cd chroma.js
npm install
Then compile the coffee-script source files to the build files:
npm run build
Don't forget to tests your changes! You will probably also want to add new test to the /test
folder in case you added a feature.
npm test
And to update the documentation just run
npm run docs
To preview the docs locally you can use
npm run docs-preview
Similar Libraries / Prior Art
Author
Chroma.js is written by Gregor Aisch.
License
Released under BSD license.
Versions prior to 0.4 were released under GPL.
Further reading
FAQ
There have been no commits in X weeks. Is chroma.js dead?
No! It's just that the author of this library has other things to do than devoting every week of his life to making cosmetic changes to a piece of software that is working just fine as it is, just so that people like you don't feel like it's abandoned and left alone in this world to die. Bugs will be fixed. Some new things will come at some point. Patience.